﻿@model ViewModels.Shared.PhoneModel

<div class="form-group">
	<label>手机号码：（* 必填）</label>
	<div class="input-group">
		<span class="input-group-prepend">
			<span class="input-group-text d-flex justify-content-center align-items-center">+86</span>
		</span>
		<input asp-for="Number" class="form-control" placeholder="用于获取短信验证码" zyf-phone-number="" />
	</div>
	<span asp-validation-for="Number"></span>
</div>
<div class="form-group">
	<label>我不是机器人：（* 必填）</label><br />
	<partial name="../Captcha/_Get" controller="Captcha">
</div>
<div class="form-group">
	<label>短信验证码：（* 必填）</label>
	<div class="input-group">
		<span class="input-group-prepend">
			<button zyf-phone-checkcode-send type="button" class="btn btn-outline-secondary">点击获取</button>
		</span>
		<input asp-for="CheckCode" class="form-control" placeholder="由【重庆首顾科技】发送，4位数字" zyf-phone-checkcode="" disabled="true" />
	</div>
	<span asp-validation-for="CheckCode"></span>
</div>

<script>

	$(function () {
		let sendStatus = "(<span>60</span>)";

		$('[zyf-phone-number]').focusout(function () {
			var $this = $(this);
			if ($('form').validate().element($this)) {
				$.ajax({
					url: '/api/Phone/Exist?number=' + $this.val(),
					success: function (data) {
						checkPhoneNumber(data);
					},
					error: JqueryAjaxError
				})
			}
		});

		$("[zyf-phone-checkcode-send]").click(function () {
			var $this = $(this),
				timer = 60,
				sendText = '点击获取',
				$form = $this.parents('form'),
				$input = $form.find("[zyf-captcha-input]"),
				$number = $form.find("[zyf-phone-number]");
			if ($this.text() != sendText) {
				return;
			}
			if (getNumberErrorMessage().text()) {    //不能省略
				return;
			}
			if (!$form.valid()) {
				return;
			}

			$('[zyf-phone-checkcode]').attr("disabled", true);

			$.ajax({
				url: "/api/Phone/Send?captcha=" + $input.val() + "&number=" + $number.val(),
				success: function (data) {
					if (!hasError(data)) {
						$("[zyf-byPhone-submit]").removeAttr("disabled");
						$this.html(sendStatus);
						$('[zyf-phone-checkcode]').removeAttr('disabled');
						var intervaler = setInterval(function () {
							timer--;
							$this.children('span').text(timer);
							if (timer <= 0) {
								clearInterval(intervaler);
								$('[zyf-phone-checkcode]').removeAttr("disabled");
								$this.text(sendText);
							}
						}, 1000)
					} else {
						initCaptcha(true);
						$('[zyf-captcha-input]').val("").focus();
						parseAjaxError(data);
					}
				},
				error: function (jqXHR, textStatus, errorThrown) {
					JqueryAjaxError(jqXHR, textStatus, errorThrown);
					showRemind(getWarningIcon() + "短信发送失败，可以稍后重试或联系我们");
				},
				complete: function () {
					initCaptcha(true);
				}
			})
		})

	})

	function getNumberErrorMessage() {
		return $('[data-valmsg-for="Number"]');
	}

</script>